/*     */ package com.lmpop.trade.uploadFile;
/*     */ 
/*     */ import com.lmpop.frame.util.Config;
/*     */ import com.lmpop.frame.util.RandomID;
/*     */ import com.lmpop.trade.ti_attach.Ti_attach;
/*     */ import com.lmpop.trade.ti_attach.Ti_attachInfo;
/*     */ import com.lmpop.trade.util.ImgYin;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Date;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServlet;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import javax.servlet.http.HttpSession;
/*     */ import org.apache.commons.fileupload.FileItem;
/*     */ import org.apache.commons.fileupload.FileUploadException;
/*     */ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
/*     */ import org.apache.commons.fileupload.servlet.ServletFileUpload;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class UploadServlet extends HttpServlet
/*     */ {
/*     */   RandomID comm;
/*     */   Config config;
/*     */   Ti_attachInfo attachInfo;
/*     */   private static final long serialVersionUID = 1L;
/*  41 */   private static Logger logger = Logger.getLogger(UploadServlet.class);
/*     */ 
/*     */   public UploadServlet()
/*     */   {
/*  46 */     this.comm = new RandomID();
/*  47 */     this.attachInfo = new Ti_attachInfo();
/*     */   }
/*     */ 
/*     */   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
/*     */     throws ServletException, IOException
/*     */   {
/*  55 */     doPost(req, resp);
/*     */   }
/*     */ 
/*     */   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
/*     */     throws ServletException, IOException
/*     */   {
/*  67 */     PrintWriter out = resp.getWriter();
/*  68 */     Config coreConfig = new Config();
/*  69 */     String rootpath = coreConfig.getString("rootpath");
/*     */ 
/*  71 */     Config uploaderConfig = new Config();
/*     */ 
/*  73 */     RandomID randomID = new RandomID();
/*     */ 
/*  75 */     String imgsavepath = uploaderConfig.getString("imgsavepath");
/*  76 */     String water_img = uploaderConfig.getString("water_img");
/*  77 */     String waterpic_path = uploaderConfig.getString("waterpic_path");
/*  78 */     String softpath = uploaderConfig.getString("softpath");
/*  79 */     int filesize = Integer.parseInt(uploaderConfig.getString("filesize"));
/*  80 */     String temp_path = uploaderConfig.getString("temppath");
/*  81 */     String img_temp = uploaderConfig.getString("img_temp");
/*  82 */     String img_ext = uploaderConfig.getString("imagesext");
/*     */ 
/*  84 */     String visit_from = req.getHeader("referer");
/*     */ 
/*  86 */     HttpSession session = req.getSession();
/*  87 */     String cust_id = ""; String user_id = "";
/*  88 */     if (session.getAttribute("session_cust_id") != null) {
/*  89 */       cust_id = session.getAttribute("session_cust_id").toString();
/*     */     }
/*  91 */     if (session.getAttribute("session_user_id") != null) {
/*  92 */       user_id = session.getAttribute("session_user_id").toString();
/*     */     }
/*     */ 
/*  95 */     String savePath = rootpath + imgsavepath + cust_id;
/*  96 */     String tempPath = rootpath + temp_path;
/*  97 */     String file_path_db = imgsavepath + cust_id;
/*  98 */     String saveFilePath = "";
/*     */ 
/* 101 */     long MAX_SIZE = filesize * 1024 * 1024;
/*     */ 
/* 105 */     logger.debug("The Savepath is :" + savePath);
/*     */ 
/* 107 */     File file = new File(savePath);
/* 108 */     if (!file.exists()) {
/* 109 */       file.mkdirs();
/* 110 */       logger.debug("Create Fire Directory");
/*     */     }
/*     */ 
/* 113 */     DiskFileItemFactory diskFileFactory = new DiskFileItemFactory();
/* 114 */     diskFileFactory.setSizeThreshold(4096);
/* 115 */     diskFileFactory.setRepository(new File(tempPath));
/* 116 */     ServletFileUpload fileUpload = new ServletFileUpload(diskFileFactory);
/* 117 */     fileUpload.setHeaderEncoding("UTF-8");
/* 118 */     fileUpload.setSizeMax(MAX_SIZE);
/*     */ 
/* 120 */     List fileList = null;
/*     */     try
/*     */     {
/* 123 */       fileList = fileUpload.parseRequest(req);
/*     */     }
/*     */     catch (FileUploadException e) {
/* 126 */       e.printStackTrace();
/* 127 */       logger.debug("fileUpload.parseRequest(req)");
/* 128 */       return;
/*     */     }
/*     */ 
/* 131 */     Iterator iterator = fileList.iterator();
/*     */ 
/* 133 */     Iterator fieldIterator = fileList.iterator();
/*     */ 
/* 135 */     String fileName = "";
/* 136 */     String fileExtName = "";
/* 137 */     String attach_root_id = req.getParameter("attach_root_id");
/*     */ 
/* 139 */     String response_str = "";
/*     */ 
/* 141 */     while (iterator.hasNext()) {
/* 142 */       FileItem fileItem = (FileItem)iterator.next();
/* 143 */       if (!fileItem.isFormField())
/*     */       {
/* 145 */         fileName = fileItem.getName();
/* 146 */         long fileSize = fileItem.getSize();
/* 147 */         String fileType = fileItem.getContentType();
/* 148 */         logger.debug("fileSize: " + fileSize + "  fileType: " + fileType);
/*     */ 
/* 151 */         if ((fileName == null) && (fileName.trim().equals(""))) {
/* 152 */           logger.debug("File Name Is Null Or Empty");
/* 153 */           continue;
/*     */         }
/*     */ 
/* 158 */         if (fileName.lastIndexOf(".") >= 0) {
/* 159 */           fileExtName = fileName.substring(fileName.lastIndexOf("."));
/* 160 */           logger.debug("File Ext Name Is : " + fileExtName);
/*     */         }
/*     */ 
/* 165 */         File temFile = null;
/* 166 */         String randFileName = "";
/*     */         do
/*     */         {
/* 170 */           randFileName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
/* 171 */           randFileName = randFileName + fileExtName;
/* 172 */           file_path_db = file_path_db + "/" + randFileName;
/* 173 */           saveFilePath = savePath + "/" + randFileName;
/* 174 */           temFile = new File(saveFilePath);
/* 175 */         }while (temFile.exists());
/*     */ 
/* 177 */         File saveFile = new File(saveFilePath);
/*     */         try
/*     */         {
/* 181 */           if (img_ext.indexOf(fileExtName) >= 0)
/*     */           {
/* 183 */             fileItem.write(saveFile);
/* 184 */             response_str = file_path_db;
/* 185 */             logger.debug("The File   : " + saveFilePath + "  has saved!");
/*     */ 
/* 187 */             if (water_img.equals("0"))
/*     */             {
/* 189 */               String yin_root_path = rootpath + waterpic_path;
/* 190 */               ImgYin.pressImage(yin_root_path, saveFilePath);
/*     */             }
/*     */ 
/* 194 */             response_str = response_str + "|" + insertAttach(attach_root_id, randFileName, "0", file_path_db, "", "1", cust_id, user_id);
/*     */           }
/*     */           else
/*     */           {
/* 199 */             response_str = "333";
/*     */           }
/*     */         }
/*     */         catch (Exception e) {
/* 203 */           e.printStackTrace();
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/* 208 */     logger.debug("The response_str   : " + response_str);
/* 209 */     out.write(response_str);
/* 210 */     out.close();
/*     */   }
/*     */ 
/*     */   public String insertAttach(String attach_root_id, String attach_name, String file_type, String file_path, String attach_desc, String validity, String cust_id, String user_id)
/*     */   {
/* 217 */     Ti_attach ti_attach = new Ti_attach();
/* 218 */     String attach_id = this.comm.GenTradeId();
/* 219 */     ti_attach.setAttach_id(attach_id);
/* 220 */     ti_attach.setAttach_root_id(attach_root_id);
/* 221 */     ti_attach.setAttach_name(attach_name);
/* 222 */     ti_attach.setFile_type(file_type);
/* 223 */     ti_attach.setFile_path(file_path);
/* 224 */     ti_attach.setAttach_desc(attach_desc);
/* 225 */     ti_attach.setValidity(validity);
/* 226 */     ti_attach.setCust_id(cust_id);
/* 227 */     ti_attach.setUser_id(user_id);
/* 228 */     this.attachInfo.insertAttach(ti_attach);
/* 229 */     return attach_id;
/*     */   }
/*     */ }

/* Location:           F:\电商\commons-utils.jar
 * Qualified Name:     com.lmpop.trade.uploadFile.UploadServlet
 * JD-Core Version:    0.6.0
 */